package com.zzl.bonds.ticket.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzl.bonds.ticket.dao.BusinessContractDao;
import com.zzl.bonds.ticket.dto.BusinessContractAddDto;
import com.zzl.bonds.ticket.dto.BusinessContractUpdateDto;
import com.zzl.bonds.ticket.entity.BusinessContractEntity;
import com.zzl.bonds.ticket.service.BusinessContractService;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.serurity.entity.User;
import com.zzl.general.common.utils.PageUtils;
import com.zzl.general.common.utils.Query;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 电子合同表
 *
 * @author gull
 * @date 2022-07-06 13:43:52
 */
@Service("businessContractService")
public class BusinessContractServiceImpl extends ServiceImpl<BusinessContractDao, BusinessContractEntity> implements BusinessContractService {

    /**
     * 分页查询
     *
     * @param params 查询参数
     * @return 分页数据
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BusinessContractEntity> page = this.page(
                new Query<BusinessContractEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    /**
     * 新增
     *
     * @param businessContractAddDto 新增实体
     * @param user                   当前登录用户
     * @throws SysException 业务异常
     */
    @Override
    public void saveBusinessContract(BusinessContractAddDto businessContractAddDto, User user) throws SysException {
        BusinessContractEntity businessContractEntity = new BusinessContractEntity();
        BeanUtils.copyProperties(businessContractAddDto, businessContractEntity);
        businessContractEntity.setCreatorId(user.getId());
        if (!this.save(businessContractEntity)) {
            throw new SysException("A00001", "保存失败！");
        }
    }

    /**
     * 修改
     *
     * @param businessContractUpdateDto 修改实体
     * @param user                      当前登录用户
     * @throws SysException 业务异常
     */
    @Override
    public void updateBusinessContractById(BusinessContractUpdateDto businessContractUpdateDto, User user) throws SysException {
        //查询合同
        QueryWrapper<BusinessContractEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BusinessContractEntity::getContractId, businessContractUpdateDto.getContractId());
        List<BusinessContractEntity> list = this.list(queryWrapper);
        if (CollectionUtil.isEmpty(list)) {
            return;
        }
        BusinessContractEntity businessContractEntity1 = list.get(0);
        //修改合同
        BusinessContractEntity businessContractEntity = new BusinessContractEntity();
        businessContractEntity.setId(businessContractEntity1.getId());
        BeanUtils.copyProperties(businessContractUpdateDto, businessContractEntity);
        businessContractEntity.setUpdaterId(user.getId());
        if (!this.updateById(businessContractEntity)) {
            throw new SysException("A00002", "修改失败！");
        }
    }

    /**
     * 通过票的id查 相关合同
     *
     * @param businessId 票据id
     * @param code       业务场景枚举
     * @return
     */
    @Override
    public List<BusinessContractEntity> getByTicketId(String businessId, Integer code) {
        QueryWrapper<BusinessContractEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BusinessContractEntity::getBusinessId, businessId)
                .eq(BusinessContractEntity::getBusinessType, code);
        return this.list(queryWrapper);
    }

}